{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d57b6a54-73ff-4283-9826-0018b2c727e5",
   "metadata": {},
   "source": [
    "## 高斯核的采样过程"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee8756c6-ce15-4fbf-b1ff-8ba9da596494",
   "metadata": {},
   "source": [
    "### 主要内容：\n",
    "* 确定高斯模板的大小，为基数，此题要求尺寸为5.\n",
    "* 计算该高斯模板坐标。\n",
    "* 确定高斯函数方差大小，如为2.25，将各坐标点对应的高斯函数值求出，得到高斯小数模板。\n",
    "* 再对高斯小数模板进行归一化处理。\n",
    "* 最后通过整形化处理，得到高斯整数模板。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7486ddca-99e6-4441-966e-1c450ed7bd8d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始化高斯模板坐标...大小为5×5...\n",
      "[(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2)]\n",
      "[(-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2)]\n",
      "[(0, -2), (0, -1), (0, 0), (0, 1), (0, 2)]\n",
      "[(1, -2), (1, -1), (1, 0), (1, 1), (1, 2)]\n",
      "[(2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]\n",
      "\n",
      "高斯模板如下:\n",
      " [[0.01195525 0.02328564 0.02908025 0.02328564 0.01195525]\n",
      " [0.02328564 0.04535423 0.05664058 0.04535423 0.02328564]\n",
      " [0.02908025 0.05664058 0.07073553 0.05664058 0.02908025]\n",
      " [0.02328564 0.04535423 0.05664058 0.04535423 0.02328564]\n",
      " [0.01195525 0.02328564 0.02908025 0.02328564 0.01195525]]\n",
      "\n",
      "正在进行归一化...权重和为1...\n",
      "[[0.01441882 0.02808402 0.0350727  0.02808402 0.01441882]\n",
      " [0.02808402 0.05470021 0.06831229 0.05470021 0.02808402]\n",
      " [0.0350727  0.06831229 0.08531173 0.06831229 0.0350727 ]\n",
      " [0.02808402 0.05470021 0.06831229 0.05470021 0.02808402]\n",
      " [0.01441882 0.02808402 0.0350727  0.02808402 0.01441882]]\n",
      "\n",
      "整形化高斯模板...\n",
      "[[1 1 2 1 1]\n",
      " [1 3 4 3 1]\n",
      " [2 4 5 4 2]\n",
      " [1 3 4 3 1]\n",
      " [1 1 2 1 1]]    1/53\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def gauss(x, y, u=0, s=1.5):\n",
    "    \"\"\"\n",
    "    输入x, y,均值及标准差生成高斯函数对应的值\n",
    "    :param x:x坐标\n",
    "    :param y:y坐标\n",
    "    :param u:均值\n",
    "    :param s:sigma标准差\n",
    "    :return:结果\n",
    "    \"\"\"\n",
    "    g = (1 / (2 * np.pi * s ** 2)) * np.exp(-(((x - u) ** 2 + (y - u) ** 2) / (2 * s ** 2)))\n",
    "    return g\n",
    "\n",
    "\n",
    "def make_template(k=5):\n",
    "    \"\"\"\n",
    "    输入模板要求为奇数, 生成对应的x, y坐标,\n",
    "    然后将x, y坐标拿进去生成高斯模板,\n",
    "    最后reshape\n",
    "    :param k:模板的大小\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    print(\"初始化高斯模板坐标...大小为{}×{}...\".format(k, k))\n",
    "    # 找到行与列的关系 用于生成横纵坐标\n",
    "    if k % 2 == 1:\n",
    "        t = (k - 1) // 2\n",
    "        # 坐标的范围\n",
    "        m = np.arange(-t, t + 1)\n",
    "        # 重复得到x坐标\n",
    "        # x = np.array([k * [i] for i in range(-t, t + 1)]).flatten()\n",
    "        x = np.repeat(m, k)\n",
    "        # 重复得到y坐标\n",
    "        # y = np.array(k * [i for i in range(-t, t + 1)])\n",
    "        y = np.repeat(m.reshape(1, -1), k, axis=0).flatten()\n",
    "        # 利用zip得到坐标数组\n",
    "        point = list(zip(x, y))\n",
    "        # 循环输出坐标, 调整成行和列的形式\n",
    "        for i in range(k):\n",
    "            print(point[i * k:i * k + k])\n",
    "        return x, y\n",
    "    else:\n",
    "        print(\"请正确输入模板大小...\")\n",
    "\n",
    "\n",
    "def normalization(arr):\n",
    "    \"\"\"\n",
    "    输入arr, 归一化权重和为1\n",
    "    :param arr:待归一化矩阵\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    print(\"\\n正在进行归一化...权重和为1...\")\n",
    "    arr = arr / np.sum(arr)\n",
    "    print(arr)\n",
    "    return arr\n",
    "\n",
    "\n",
    "def integer(arr):\n",
    "    \"\"\"\n",
    "    输入arr, 将其转换成整数高斯模板\n",
    "    :param arr:归一化后的高斯模板\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    print(\"\\n整形化高斯模板...\")\n",
    "    # 取第一个值 然后将左上角第一个值变成1 其它的值对应改变 并转换成整形\n",
    "    v = arr[0][0]\n",
    "    arr = np.int32(arr / v)\n",
    "    s = np.sum(arr)\n",
    "    print(arr, '   1/' + str(np.sum(arr)))\n",
    "    return arr\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    # 设置高斯模板大小, 模板请输入奇数\n",
    "    kernel = 5\n",
    "    # 初始化高斯模板\n",
    "    x, y = make_template(k=kernel)\n",
    "    # 设置高斯函数的均值和标准差\n",
    "    mean = 0\n",
    "    sigma = 1.5\n",
    "    # 得到结果\n",
    "    result = gauss(x, y, u=mean, s=sigma)\n",
    "    # reshape\n",
    "    gauss_template = np.reshape(result, (kernel, kernel))\n",
    "    print(\"\\n高斯模板如下:\\n\", gauss_template)\n",
    "    # 归一化\n",
    "    arr_nor = normalization(gauss_template)\n",
    "    # 整数化\n",
    "    arr_int = integer(arr_nor)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
